package com.tn.controller.camera;

import com.tn.controller.alarm.common.constants.Constants;
import com.tn.controller.alarm.common.enums.CommonCodeEnum;
import com.tn.controller.alarm.common.model.CommonVO;
import com.tn.controller.alarm.common.model.PageInput;
import com.tn.controller.alarm.common.model.PageResult;
import com.tn.controller.alarm.common.model.camera.entity.PositionAreaDO;
import com.tn.controller.alarm.common.model.camera.vo.AreaVideoVO;
import com.tn.controller.alarm.common.model.camera.vo.CameraVO;
import com.tn.controller.alarm.common.model.camera.vo.TimeOfTruckVO;
import com.tn.controller.alarm.common.model.camera.vo.TruckTrialCameraVO;
import com.tn.exception.DatabaseOperationFailedException;
import com.tn.exception.InvalidParamException;
import com.tn.service.camera.ICameraService;
import com.tn.utils.CheckParamUtil;
import com.tn.utils.HttpRequestSenderUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 摄像头监控管理
 *
 * @program: tn_aucy_java
 * @author: yangjiayao
 * @create: 2022/1/7
 */
@Slf4j
@RestController
@RequestMapping("/aucy")
public class CameraController {
    /**
     * 查询code类型摄像头列表URL
     */
    @Value("${externalSys.camera-system.code-camera-list}")
    private String code_camera_url;

    /**
     * service
     */
    private ICameraService cameraService;

    /**
     * 注入
     *
     * @param cameraService cameraService
     */
    @Autowired
    public void setCameraService(ICameraService cameraService) {
        this.cameraService = cameraService;
    }

    /**
     * 查询场景区域
     *
     * @return 场景区域
     */
    @RequestMapping(value = "/queryArea", method = RequestMethod.GET)
    public CommonVO<List<PositionAreaDO>> queryArea() {
        CommonVO<List<PositionAreaDO>> result = new CommonVO<>();
        result.setData(cameraService.queryArea());
        return result;
    }

    /**
     * 新增摄像头
     *
     * @param cameraVO 摄像头信息
     * @return 新增结果
     * @throws DatabaseOperationFailedException 数据库操作异常
     * @throws InvalidParamException            参数异常
     */
    @RequestMapping("/addCamera")
    public CommonVO<Object> addCamera(@RequestBody CameraVO cameraVO)
            throws DatabaseOperationFailedException, InvalidParamException {
        CheckParamUtil.checkNullRequiredParam(cameraVO.getKind(), cameraVO.getAreaId());
        CheckParamUtil.checkBlankRequiredParam(cameraVO.getFencePosition(), cameraVO.getName());
        checkCameraKind(cameraVO);
        cameraService.addCamera(cameraVO);
        return new CommonVO<>();
    }

    /**
     * 删除摄像头
     *
     * @param cameraVO 摄像头信息
     * @return 删除结果
     * @throws DatabaseOperationFailedException 数据库操作异常
     * @throws InvalidParamException            参数异常
     */
    @RequestMapping("/deleteCamera")
    public CommonVO<Object> deleteCamera(@RequestBody CameraVO cameraVO)
            throws DatabaseOperationFailedException, InvalidParamException {
        CheckParamUtil.checkNullRequiredParam(cameraVO.getId());
        cameraService.deleteCamera(cameraVO);
        return new CommonVO<>();
    }

    /**
     * 修改摄像头
     *
     * @param cameraVO 摄像头信息
     * @return 修改结果
     * @throws DatabaseOperationFailedException 数据库操作异常
     * @throws InvalidParamException            参数异常
     */
    @RequestMapping("/modifyCamera")
    public CommonVO<Object> modifyCamera(@RequestBody CameraVO cameraVO)
            throws DatabaseOperationFailedException, InvalidParamException {
        CheckParamUtil.checkNullRequiredParam(cameraVO.getId(), cameraVO.getAreaId(), cameraVO.getKind());
        CheckParamUtil.checkBlankRequiredParam(cameraVO.getFencePosition(), cameraVO.getName());
        checkCameraKind(cameraVO);
        cameraService.modifyCamera(cameraVO);
        return new CommonVO<>();
    }

    /**
     * 查询摄像头
     *
     * @param pageCamera 分页查询条件
     * @return 查询结果
     * @throws InvalidParamException 参数异常
     */
    @RequestMapping("/queryCamera")
    public CommonVO<PageResult<CameraVO>> queryCamera(@RequestBody PageInput<CameraVO> pageCamera)
            throws InvalidParamException {
        CheckParamUtil.checkPageParam(pageCamera.getCurPage(), pageCamera.getPageSize());
        CommonVO<PageResult<CameraVO>> result = new CommonVO<>();
        result.setData(cameraService.queryCamera(pageCamera));
        return result;
    }

    /**
     * 透传摄像头子系统code类型的摄像头列表
     *
     * @return 透传信息
     */
    @RequestMapping(value = "/queryCodeTypeCameraList", method = RequestMethod.GET,
            produces = "application/json;charset=UTF-8")
    public String queryCodeTypeCameraList() {
        return HttpRequestSenderUtil.get(code_camera_url);
    }

    /**
     * 查询摄像头分区域列表
     *
     * @return 列表
     */
    @RequestMapping(value = "/queryVideoList", method = RequestMethod.GET)
    public CommonVO<List<AreaVideoVO>> queryVideoList() {
        CommonVO<List<AreaVideoVO>> result = new CommonVO<>();
        result.setData(cameraService.queryVideoList());
        return result;
    }

    /**
     * 查询货车列表
     *
     * @param page 分页查询条件
     * @return 结果
     * @throws InvalidParamException 参数异常
     */
    @RequestMapping("/queryCameraTruckList")
    public CommonVO<PageResult<String>> queryCameraTruckList(@RequestBody PageInput<Object> page)
            throws InvalidParamException {
        CheckParamUtil.checkPageParam(page.getCurPage(), page.getPageSize());
        CommonVO<PageResult<String>> result = new CommonVO<>();
        result.setData(cameraService.queryTruck(page));
        return result;
    }

    /**
     * 查询货车进出场时间列表
     *
     * @param param 分页查询条件
     * @return 结果
     * @throws InvalidParamException 参数异常
     */
    @RequestMapping("/queryTimeListOfTruck")
    public CommonVO<PageResult<TimeOfTruckVO>> queryTimeListOfTruck(@RequestBody PageInput<Map<String, String>> param)
            throws InvalidParamException {
        CheckParamUtil.checkPageParam(param.getCurPage(), param.getPageSize());
        CheckParamUtil.checkNullRequiredParam(param.getData());
        CheckParamUtil.checkBlankRequiredParam(param.getData().get("license"));
        CommonVO<PageResult<TimeOfTruckVO>> result = new CommonVO<>();
        result.setData(cameraService.queryTimeOfTruck(param));
        return result;
    }

    /**
     * 查询货车经过摄像头视频列表
     *
     * @param param 分页查询条件
     * @return 结果
     * @throws InvalidParamException 参数异常
     */
    @RequestMapping("/queryTruckTrialCamera")
    public CommonVO<PageResult<TruckTrialCameraVO>> queryTruckTrialCamera(@RequestBody PageInput<TimeOfTruckVO> param)
            throws InvalidParamException {
        CheckParamUtil.checkPageParam(param.getCurPage(), param.getPageSize());
        CheckParamUtil.checkNullRequiredParam(param.getData(), param.getData().getTruckId());
        CheckParamUtil.checkBlankRequiredParam(param.getData().getInTime());
        CommonVO<PageResult<TruckTrialCameraVO>> result = new CommonVO<>();
        result.setData(cameraService.queryTruckTrialCamera(param));
        return result;
    }

    /**
     * 根据摄像头类型校验入参
     *
     * @param cameraVO 入参
     * @throws InvalidParamException 参数异常
     */
    private void checkCameraKind(CameraVO cameraVO) throws InvalidParamException {
        if (Constants.CODE_TYPE_CAMERA == cameraVO.getKind()) {
            CheckParamUtil.checkBlankRequiredParam(cameraVO.getCode());
            if (StringUtils.isNotBlank(cameraVO.getIp())) {
                throw new InvalidParamException(CommonCodeEnum.PARAM.getCode(), CommonCodeEnum.PARAM.getMessage());
            }
        } else if (Constants.IP_TYPE_CAMERA == cameraVO.getKind()) {
            CheckParamUtil.checkBlankRequiredParam(cameraVO.getIp(), cameraVO.getUserName(), cameraVO.getPassWord());
            if (StringUtils.isNotBlank(cameraVO.getCode())) {
                throw new InvalidParamException(CommonCodeEnum.PARAM.getCode(), CommonCodeEnum.PARAM.getMessage());
            }
        } else {
            throw new InvalidParamException(CommonCodeEnum.PARAM.getCode(), CommonCodeEnum.PARAM.getMessage());
        }
    }


    /**
     * 新增区域
     *
     * @param positionAreaDO 摄像头信息
     * @return 新增结果
     * @throws DatabaseOperationFailedException 数据库操作异常
     * @throws InvalidParamException            参数异常
     */
    @RequestMapping("/addArea")
    public CommonVO<Object> addArea(@RequestBody PositionAreaDO positionArea)
    {
        cameraService.addArea(positionArea);
        return new CommonVO<>();
    }

    /**
     * 编辑区域
     *
     * @param positionArea 摄像头信息
     * @return 新增结果
     * @throws DatabaseOperationFailedException 数据库操作异常
     * @throws InvalidParamException            参数异常
     */
    @RequestMapping("/editArea")
    public CommonVO<Object> editArea(@RequestBody PositionAreaDO positionArea)
    {
        cameraService.editArea(positionArea);
        return new CommonVO<>();
    }

    /**
     * 删除区域
     *
     * @param id 区域信息
     * @return 新增结果
     * @throws DatabaseOperationFailedException 数据库操作异常
     * @throws InvalidParamException            参数异常
     */
    @GetMapping("/deleteArea/{id}")
    public CommonVO<Object> deleteArea(@PathVariable("id") Integer id)
    {
        cameraService.deleteArea(id);
        return new CommonVO<>();
    }

}
